package com.lmsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmsservice.common.R;
import com.lmsservice.entity.Records;
import com.lmsservice.entity.User;
import com.lmsservice.service.RecordService;
import com.lmsservice.service.UserService;
import com.lmsservice.utils.JWTUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RecordService recordService;

    @PostMapping("/login")
    public R<User> login(HttpServletResponse response, @RequestBody User user){

        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,user.getAccount());
        User user1 = userService.getOne(queryWrapper);
        if (user1==null){
            return R.error("用户不存在");
        }

        if (!user1.getPassword().equals(password)){
            return R.error("密码错误");
        }

        String token = JWTUtil.sign(user);
        response.setHeader("token",token);

        return R.success(user1);
    }

    @PostMapping("/register")
    public R<String> register(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,user.getAccount());
        User user1 = userService.getOne(queryWrapper);
        if (user1!=null){
            return R.error("账号已注册");
        }
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setName(user.getAccount());
        userService.save(user);

        return R.success("注册成功");
    }

    @GetMapping()
    public R<Page> getUser(Integer page, Integer pageSize , String account,String name,String phone){
        Page<User> userPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!Objects.equals(account, ""),User::getAccount,account)
                .like(!Objects.equals(name, ""),User::getName,name)
                .like(!Objects.equals(phone, ""),User::getPhone,phone);
        userService.page(userPage,queryWrapper);
        return R.success(userPage);
    }

    @PutMapping
    public R<String> updateById(@RequestBody User user){

        if (!Objects.equals(user.getPassword(), "")){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        } else {
            User user1 = userService.getById(user.getId());
            user.setPassword(user1.getPassword());
        }
        userService.updateById(user);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deleteById(@RequestBody User user){

        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user.getId()!=0,Records::getUserId,user.getId());
        List<Records> records = recordService.list(queryWrapper);
        recordService.removeBatchByIds(records);
        userService.removeById(user);
        return R.success("删除成功");
    }

    @PostMapping
    public R<String> addCustomer(@RequestBody User user){
//        MD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userService.save(user);
        return R.success("添加成功");
    }

}
